<template>
	<el-card shadow="never" header="修改密码">
		<el-alert title="密码更新成功后，您将被重定向到登录页面，您可以使用新密码重新登录。" type="info" show-icon style="margin-bottom: 15px;"/>
		<el-form ref="epForm" :model="epForm" :rules="epRules" label-width="120px" style="margin-top:20px;">
			<el-form-item label="当前密码" prop="password">
				<el-input v-model="epForm.password" type="password" show-password placeholder="请输入当前密码"></el-input>
				<div class="el-form-item-msg">必须提供当前登录用户密码才能进行更改</div>
			</el-form-item>
			<el-form-item label="新密码" prop="newPass">
				<el-input v-model="epForm.newPass" type="password" show-password placeholder="请输入新密码"></el-input>
				<sc-password-strength v-model="epForm.newPass"></sc-password-strength>
				<div class="el-form-item-msg">请输入包含英文、数字的8位以上密码</div>
			</el-form-item>
			<el-form-item label="确认新密码" prop="cnewPass">
				<el-input v-model="epForm.cnewPass" type="password" show-password placeholder="请再次输入新密码"></el-input>
			</el-form-item>
			<el-form-item>
				<el-button type="primary" @click="passwordEditSubmit">保存密码</el-button>
			</el-form-item>
		</el-form>
	</el-card>
</template>

<script>
import scPasswordStrength from '@/components/scPasswordStrength'

export default {
	name: "password",
	components: {
		scPasswordStrength
	},
	data(){
		return{
			epForm:{
				password:"",
				newPass:"",
				cnewPass:""
			},
			epRules:{
				password:[
					{required: true, message: '请输入原密码'}
				],
				newPass: [
					{required: true, message: '请输入新密码'},
					{validator: (rule, value, callback) => {
							if (this.epForm.cnewPass !== '') {
								this.$refs.epForm.validateField('cnewPass');
							}
							callback();
						}}
				],
				cnewPass: [
					{required: true, message: '请再次输入新密码'},
					{validator: (rule, value, callback) => {
							if (value !== this.epForm.newPass) {
								callback(new Error('两次输入密码不一致!'));
							}else{
								callback();
							}
						}}
				],
			},
		}
	},
	methods:{
		passwordEditSubmit(){
			this.$refs.epForm.validate(async (valid) => {
				if (valid) {
					this.isSaving = true;
					var params = {password:this.epForm.password,newPass:this.epForm.cnewPass}
					var res = await this.$API.system.user.editPassword.post(params);
					this.isSaving = false;
					if(res.success){
						this.$message.success("修改密码成功！请重新登录")
						//退出登录
						this.$TOOL.data.clear()
						this.$router.replace({path: '/login'})
					}
				}else{
					return false;
				}
			})
		},
	}
}
</script>

<style scoped>

</style>
